//
//  SecondViewController.m
//  豆瓣
//
//  Created by hy on 15/12/31.
//  Copyright © 2015年 hongyu. All rights reserved.
//

#import "SecondViewController.h"
#import "SanTableViewCell.h"
#import "MoiveModel.h"
#import "UIImageView+WebCache.h"
#import "XqViewController.h"
#import "CollecionViewController.h"
#import "HyModel.h"
#import "HyCollectionViewCell.h"
#import "UIImageView+WebCache.h"
@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *marr;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *ModelArray;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UIView *aview;
@end

@implementation SecondViewController

- (void)dealloc {
    [_array release];
    [_table release];
    [_ModelArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, -5, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStylePlain];
    _table.rowHeight = 100;
    _table.separatorColor = [UIColor purpleColor];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_table];
    [_table release];
    
    _table.delegate =self;
    _table.dataSource = self;
    [_table registerClass:[SanTableViewCell class] forCellReuseIdentifier:@"cell"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    self.array = [NSMutableArray arrayWithContentsOfFile:path];
    self.ModelArray = [NSMutableArray array];
    for (NSDictionary * dic in _array) {
        MoiveModel *model = [MoiveModel moivemodel];
        [model setValuesForKeysWithDictionary:dic];
        [_ModelArray addObject:model];
        
        [self creatItem];
    }
}
- (void)creatItem {
    UIImage *image = [UIImage imageNamed:@"zs.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image landscapeImagePhone:[UIImage imageNamed:@"zs.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    [item release];
}
- (void)itemAction:(UIBarButtonItem *)item {
    self.aview = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_aview];
    [self creatcollection];
    [self fhitembutton];
    [self data];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _ModelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.lanImage.image = [UIImage imageNamed:@"bg_eventlistcell.png"];
    MoiveModel *model = _ModelArray[indexPath.row];
    cell.wenLable.text = model.title;
    [cell.moiveimage sd_setImageWithURL:[NSURL URLWithString:model.image]];
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 190;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XqViewController *vc = [[XqViewController alloc] init];
    vc.dic = _array[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


/**数据解析*/
- (void)data {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    self.marr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        HyModel *model = [HyModel model];
        [model setValuesForKeysWithDictionary:dic];
        [_marr addObject:model];
    }
}

- (void)creatcollection {
    //  创建瀑布流样式
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //    滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    item大小
    layout.itemSize = CGSizeMake((375 - 40) / 3, 150);
    //    行高列高
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //    创建collectionview
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    collection.backgroundColor = [UIColor whiteColor];
    [_aview addSubview:collection];
    [collection release];
    [layout release];
    [collection registerClass:[HyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collection.delegate = self;
    collection.dataSource = self;
}
- (void)fhitembutton {
    UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zs.png"] style:UIBarButtonItemStylePlain target:self action:@selector(item5Action:)];
    self.navigationItem.rightBarButtonItem = item5;
    [item5 release];
}
//itembutton方法
- (void)item5Action:(UIBarButtonItem *)item {
    [self.aview removeFromSuperview];
    [self creatItem];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _marr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HyModel *model = _marr[indexPath.row];
    [cell.hyImageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"111.jpg"]];
    cell.hyLabel.text = model.title;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    XqViewController *vc = [[XqViewController alloc] init];
    vc.dic = _array[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
